package com.bookmanager.www.common.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bookmanager.www.common.Constants.PreferentialConstant;
import com.bookmanager.www.entity.FlUser;
import com.bookmanager.www.mapper.FlUserMapper;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 风离
 * @Date: 2021/10/02/13:48
 * @Description: 用户登录封装 重复的生成Token在此完成
 */
@Slf4j

public class LoginUserUtils {



    public static String getJWT(String userId)
    {
        JwtUtil jwtUtil = new JwtUtil("userId", SignatureAlgorithm.HS256);

        Map hasher = new HashMap();
        hasher.put("userId",userId);
        String jwt = jwtUtil.encode("fl1906",1000*60*60*24*7,hasher);

        return jwt;
    }


    /**
     * 根据ip获取用户详细地理位置！！
     * @param ip
     * @return
     */
    public static String getUserDetail(String ip)
    {

        String url = "http://apis.juhe.cn/ip/ipNew?ip="+ip+"&key="+ PreferentialConstant.ipKey;
        String result = HttpUtil.get(url);
        JSONObject resultJSON = JSONUtil.parseObj(result);
        String resultCode=resultJSON.getStr("resultcode");
        if(resultCode.equals("201"))
        {
            return "error";
        }
        Object object;
        String reason = resultJSON.getStr("reason");
        String resultObj=resultJSON.getStr("result");
        JSONObject rj=JSONUtil.parseObj(resultObj);
        String country = rj.getStr("Country");
        String province = "";
        String city = "";
        String isp = "";
        if(rj.getStr("Province")!=null)
        {
            province=rj.getStr("Province");
        }
        if(rj.getStr("City")!=null)
        {
            city=rj.getStr("City");
        }
        if(rj.getStr("Isp")!=null)
        {
            isp=rj.getStr("Isp");
        }
        String allAddress  = country+"-"+province+"-"+city+"-"+isp;
        log.error(allAddress);
        return allAddress;
    }

    public static Result login(FlUser flUser, HttpServletResponse response)
    {
        if(flUser!=null)
        {
            //获取用户id
            String userId = flUser.getUserId();


            //设置需要加密的秘钥 和加密的算法

            /**
             *  # 加密密钥
             *    secret: abcdefg1234567
             */
            JwtUtil jwtUtil = new JwtUtil("userId", SignatureAlgorithm.HS256);

            Map hasher = new HashMap();
            hasher.put("userId",userId);

            //token中传入加密头 和token存活时间 默认七天
            String jwt = jwtUtil.encode("fl1906",1000*60*60*24*7,hasher);

            System.out.println("生成的jwt为："+jwt);

            response.setHeader("Authorization", jwt);
            response.setHeader("Access-control-Expose-Headers", "Authorization");

            return Result.succ("登录成功！！",flUser);
        }
        return Result.fail("登录失败，账号或者密码错误！","");
    }
}
